﻿Imports System.Data.SqlClient

Public Class Frm_AchatEquipement
    Dim deClt As DictionaryEntry
    Dim detype As DictionaryEntry
    Dim idachat As Integer = -1
    Public Sub vider()
        List_Equip.Items.Clear()
        txt_bon_Achat.Clear()
        Date_achat.CustomFormat = " "
        Date_achat.Format = DateTimePickerFormat.Custom
        txt_prix.Clear()
        txt_Qnt.Clear()
        txt_prixTotl.Clear()
    End Sub

    Public Sub Selectionner(ByVal n As Integer)
        Dim idF As Integer
        Cmd = New SqlCommand("select * from achatEquipFour where idAchat=" & n, Con)
        open()
        Dr = Cmd.ExecuteReader()
        Dr.Read()
        If Dr.HasRows Then
            If Dr("dateAchat") = "1900-01-01" Then
                Date_achat.CustomFormat = " "
                Date_achat.Format = DateTimePickerFormat.Custom
            Else
                Date_achat.Value = Dr("dateAchat")
            End If
            txt_bon_Achat.Text = Dr("numBon")
            idF = Dr("idFour")
        End If
        Dr.Close()
        cnClose()
        For Each it As DictionaryEntry In Comb_Nom_Fournisseurs.Items
            If it.Key = idF Then
                Comb_Nom_Fournisseurs.SelectedItem = it
            End If
        Next
        '----------------------------------------------------
        Cmd = New SqlCommand("select categorie,libelle,equipeAchat.* from equipeAchat,trTypeEquipement,trEquipement" & _
                             " where equipeAchat.idTypeEquip=trTypeEquipement.idType" & _
                             " and trTypeEquipement.idEquipement=trEquipement.id" & _
                             " and idAchat=" & n, Con)
        open()
        Dr = Cmd.ExecuteReader()
        If Dr.HasRows Then
            Do While Dr.Read()
                Dim Line As ListViewItem = New ListViewItem(New String() {Dr("categorie"), Dr("libelle"), Dr("prixUnit"), Dr("quantite"), CDbl(Dr("prixUnit")) * CDbl(Dr("quantite")), Dr("idTypeEquip")})
                List_Equip.Items.Add(Line)
            Loop
        End If
        Dr.Close()
        cnClose()
        idachat = n
    End Sub
    Private Sub Frm_AchatEquipement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'vider DateTimePecher
        Date_achat.CustomFormat = " "
        Date_achat.Format = DateTimePickerFormat.Custom

        '-------------------------------Remplir combo fornisseur---------------------------------'
        Try
            Comb_Nom_Fournisseurs.Items.Clear()
            Dim htFour As New Hashtable()
            Cmd = New SqlCommand("Select * From Fournisseus where type='Matériels' or type='Réparations et Matériels'", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                If Dr("Type_Fournisseur") = "Entreprise" Then
                    htFour.Add(Dr("Id_Fournisseur"), Dr("Nom_entreprise"))
                Else
                    htFour.Add(Dr("Id_Fournisseur"), Dr("Nom") & "  " & Dr("Prenom"))
                End If
            Loop
            Dr.Close()
            cnClose()
            For Each ImageType As DictionaryEntry In htFour
                Comb_Nom_Fournisseurs.Items.Add(ImageType)
            Next

            Comb_Nom_Fournisseurs.DisplayMember = "value"
            Comb_Nom_Fournisseurs.ValueMember = "key"

            If Comb_Nom_Fournisseurs.Items.Count > 0 Then
                Comb_Nom_Fournisseurs.SelectedIndex = 0
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
        '-----------------------remplir combo Equipement--------------------------------
        Try
            Dim htEqui As New Hashtable()
            open()
            remplirEquip()
            For Each equi In lstEquipement
                htEqui.Add(equi.idEquip, equi.categorie)
            Next
            cnClose()
            For Each ImageType As DictionaryEntry In htEqui
                Comb_Equipement.Items.Add(ImageType)
            Next

            Comb_Equipement.DisplayMember = "value"
            Comb_Equipement.ValueMember = "key"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub Date_achat_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Date_achat.ValueChanged
        Date_achat.CustomFormat = "dd/MM/yyyy"
    End Sub

    Private Sub Comb_Nom_Fournisseurs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_Nom_Fournisseurs.SelectedIndexChanged

        deClt = DirectCast(Comb_Nom_Fournisseurs.SelectedItem, DictionaryEntry)

        '-------------------------------Remplir txt categorie---------------------------------'
        Try

            Cmd = New SqlCommand("Select Categorie From Fournisseus where id_Fournisseur=" & CInt(deClt.Key), Con)
            open()
            txt_categorie.Text = Cmd.ExecuteScalar
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub Comb_Equipement_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_Equipement.SelectedIndexChanged
        If Comb_Equipement.SelectedIndex >= 0 Then
            Comb_type.Items.Clear()
            objEquipement = lstEquipement.Where(Function(y As equipement) y.categorie = Comb_Equipement.Text)(0)
            open()
            objEquipement.mesTypes()
            cnClose()
            Dim httype As New Hashtable()
            For Each typeEqui In objEquipement.types
                httype.Add(typeEqui.idType, typeEqui.libelle)
            Next
            For Each ImageType As DictionaryEntry In httype
                Comb_type.Items.Add(ImageType)
            Next

            Comb_type.DisplayMember = "value"
            Comb_type.ValueMember = "key"
            If Comb_type.Items.Count > 0 Then
                Comb_type.SelectedIndex = 0
            End If
        End If
    End Sub

    Private Sub btn_ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter.Click
        If (Comb_Nom_Fournisseurs.Text <> "" And List_Equip.Items.Count > 0) Then
            If MsgBox("Vous Voulez Vraiment Ajouter  ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter ") = MsgBoxResult.Yes Then
                Try
                    Cmd = New SqlCommand("insert into achatEquipFour (dateAchat,numBon,idFour)" & _
                    "values (@dateAchat,@numBon,@idFour)", Con)

                    Cmd.Parameters.Add("@numBon", SqlDbType.VarChar).Value = txt_bon_Achat.Text
                    Cmd.Parameters.Add("@idFour", SqlDbType.Int).Value = CInt(deClt.Key)
                    If Date_achat.CustomFormat = " " Then
                        Cmd.Parameters.Add("@dateAchat", SqlDbType.Date).Value = "1900/01/01"
                    Else
                        Cmd.Parameters.Add("@dateAchat", SqlDbType.Date).Value = Date_achat.Value
                    End If

                    open()
                    Cmd.ExecuteNonQuery()
                    Cmd.CommandText = "select MAX(idAchat) from achatEquipFour"
                    Dim idachat As Integer = Cmd.ExecuteScalar
                    cnClose()
                    '---------------------------------------------------------------------------
                    Try
                        For i As Integer = 0 To List_Equip.Items.Count - 1
                            Cmd = New SqlCommand("insert into equipeAchat values (@idTypeEquip,@quantite,@prixUnit,@idAchat)", Con)
                            Cmd.Parameters.Add("@idTypeEquip", SqlDbType.Int).Value = List_Equip.Items(i).SubItems(5).Text
                            Cmd.Parameters.Add("@quantite", SqlDbType.Float).Value = List_Equip.Items(i).SubItems(3).Text
                            Cmd.Parameters.Add("@prixUnit", SqlDbType.Float).Value = List_Equip.Items(i).SubItems(2).Text
                            Cmd.Parameters.Add("@idAchat", SqlDbType.Int).Value = idachat

                            open()
                            Cmd.ExecuteNonQuery()
                            cnClose()
                            '----------------ajouter Qnt en stock---------------------------------
                            Cmd = New SqlCommand("update trTypeEquipement set quantiteStock = quantiteStock + " & CDbl(List_Equip.Items(i).SubItems(3).Text) & " where idType=" & CInt(List_Equip.Items(i).SubItems(5).Text), Con)
                            open()
                            Cmd.ExecuteNonQuery()
                            cnClose()
                        Next
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try

                    vider()

                    MsgBox("Ajouté Avec Succes", MsgBoxStyle.Information, "Réussi !")

                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                    cnClose()
                End Try
            End If

        Else
            MsgBox("Entrer les données", MsgBoxStyle.Critical, "Erreur !")
        End If
    End Sub

    Private Sub btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nouv.Click
        vider()
        idachat = -1
    End Sub

    Private Sub txt_prix_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_prix.KeyPress
        Dim allowedChars As String = "1234567890,"
        If txt_prix.Text.Contains(",") Then
            allowedChars = "1234567890"
        End If
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_Qnt_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Qnt.KeyPress
        Dim allowedChars As String = "1234567890,"
        If txt_Qnt.Text.Contains(",") Then
            allowedChars = "1234567890"
        End If
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_prix_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_prix.TextChanged
        Try
            If txt_prix.Text <> "" And txt_Qnt.Text <> "" Then
                txt_prixTotl.Text = CDbl(txt_prix.Text) * CDbl(txt_Qnt.Text)
            End If
            If txt_prix.Text = "" Then
                txt_prixTotl.Text = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub txt_Qnt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Qnt.TextChanged
        Try
            If txt_prix.Text <> "" And txt_Qnt.Text <> "" Then
                txt_prixTotl.Text = CDbl(txt_prix.Text) * CDbl(txt_Qnt.Text)
            End If
            If txt_Qnt.Text = "" Then
                txt_prixTotl.Text = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub btn_ajouEquip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouEquip.Click
        If Comb_Equipement.Text <> "" And txt_prix.Text <> "" And txt_Qnt.Text <> "" Then
            detype = DirectCast(Comb_type.SelectedItem, DictionaryEntry)
            Dim str As String = detype.Key
            Dim Line As ListViewItem = New ListViewItem(New String() {Comb_Equipement.Text, Comb_type.Text, txt_prix.Text, txt_Qnt.Text, txt_prixTotl.Text, str})
            List_Equip.Items.Add(Line)
            txt_prix.Clear()
            txt_Qnt.Clear()
            txt_prixTotl.Clear()
        End If
    End Sub

    Private Sub btn_suppFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_suppFour.Click
        For Each i As ListViewItem In List_Equip.SelectedItems
            List_Equip.Items.Remove(i)
        Next
    End Sub

    Private Sub btn_recherche_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_recherche.Click
        Me.Enabled = False
        Frm_Recherche_Equipements.Show()

    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        Me.Enabled = False
        rech = "achat"
        Frm_Rech_AchatEquipe.Show()
    End Sub

    Private Sub btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp.Click
        If idachat <> -1 Then
            If MsgBox("Voulez Vous Vraiment Supprimer cette achat ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Suppréssion !") = MsgBoxResult.Yes Then
                Try
                    Cmd = New SqlCommand("delete from achatEquipFour where idAchat=" & idachat, Con)
                    open()
                    Cmd.ExecuteNonQuery()
                    cnClose()
                    idachat = -1
                    MsgBox("Suppresion effectué !", MsgBoxStyle.Information, "Terminer")
                    vider()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                End Try
            End If
        End If
       

        
    End Sub
End Class